package org.example.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.mysql.jdbc.StringUtils;
import org.example.entity.Inventory;
import org.example.exception.ErrorEnum;
import org.example.exception.ServiceException;
import org.example.mapper.InventoryMapper;
import org.example.param.InventoryParam;
import org.example.service.InventoryService;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@Service
public class InventoryServiceImpl implements InventoryService {

    @Resource
    InventoryMapper inventoryMapper;

    @Override
    public Map<String, Object> in(Inventory inventory) {
        QueryWrapper queryWrapper = new QueryWrapper();
        inventoryMapper.insert(inventory);
        return success();
    }
    @Override
    public Map<String, Object> out(Inventory inventory) {
        QueryWrapper queryWrapper = new QueryWrapper();
        inventoryMapper.insert(inventory);
        return success();
    }
    @Override
    public Map<String, Object> change(Inventory inventory) {
        QueryWrapper queryWrapper = new QueryWrapper();
        inventoryMapper.insert(inventory);
        return success();
    }

    @Override
    public Map<String, Object> page(InventoryParam param) {
        IPage<Inventory> page = new Page<>(param.getCurrent(), param.getSize());
        QueryWrapper queryWrapper = new QueryWrapper();
        page =  inventoryMapper.selectPage(page, queryWrapper);
        return success(page);
    }

    @Override
    public Map<String, Object> delete(Inventory inventory) {
        inventoryMapper.deleteById(inventory);
        return success();
    }

    @Resource
    JdbcTemplate jdbcTemplate;

    @Override
    public Map<String, Object> importExcel(MultipartFile file) {
		/*
        InventoryExcelReadListener listener = new InventoryExcelReadListener();
        try {
            EasyExcel.read(file.getInputStream(), InventoryExcel.class, listener).sheet().doRead();
            List<InventoryExcel> list = listener.getList();
            String sql = "insert into inventory (name, stock,warning_stock) values (?,?,?)";
            jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
                @Override
                public void setValues(PreparedStatement ps, int i) throws SQLException {
                    InventoryExcel inventory = list.get(i);
                    ps.setObject(1, inventory.getName());
                    ps.setObject(2, inventory.getStock());
                    ps.setObject(3, inventory.getWarningStock());
                }
                @Override
                public int getBatchSize() {
                    return list.size();
                }
            });
            return success();
        } catch (IOException e) {
            throw new ServiceException(ErrorEnum.EXCEL_IMPORT_ERROR);
        }*/
        return success();
    }
}
